layui.use(['jquery', "common"], function() {
	var controller = {
		bindEvent: function(code) {
			$("#" + code + " .btn-new").off("click").on("click", function(e) {
				var data = {};
				var _thisBtn = this;
				$(this).parent().parent().parent().find("td").each(function(ix) {
					if(ix == 0 || ix == 1 || ix == 2 || ix == 3  || ix == 4) {
						var $obj = $($(this).find("input")[0]);
						data[$obj.attr("name")] = $obj.val();
					}
				});
				$.DBEditor.addField($("#dataModel").attr("data-file-id"), data, function(data) {
					$(_thisBtn).parent().parent().parent().find("td").each(function(ix) {
						if(ix == 0 || ix == 1 || ix == 2 || ix == 3  || ix == 4) {
							$(this).html($($(this).find("input")[0]).val());
						}
					});
					$(_thisBtn).find(".layui-icon").removeClass("icon-duigou").addClass("icon-bianji");
					$(_thisBtn).removeClass("btn-new").addClass("btn-edit");
				});

				
			});
			
			$("#" + code + " .btn-edit").off("click").on("click", function(e) {
				if($(this).find(".layui-icon").hasClass("icon-duigou")) {
					// ajax 更新
					var data = {};
					var _thisBtn = this;
					$(this).parent().parent().parent().find("td").each(function(ix) {
						if(ix == 0 || ix == 1 || ix == 2 || ix == 3  || ix == 4) {
							var $obj = $($(this).find("input")[0]);
							data[$obj.attr("name")] = $obj.val();
						}
					});
					$.DBEditor.updField($("#dataModel").attr("data-file-id"), data, function(data) {
						$(_thisBtn).parent().parent().parent().parent().find("td").each(function(ix) {
							if(ix == 0 || ix == 1 || ix == 2 || ix == 3  || ix == 4) {
								$(this).html($($(this).find("input")[0]).val());
							}
						});
						$(_thisBtn).find(".layui-icon").removeClass("icon-duigou").addClass("icon-bianji");
					});

				} else {
					$(this).find(".layui-icon").removeClass("icon-bianji").addClass("icon-duigou");
					$(this).parent().parent().parent().find("td").each(function(ix) {
						if(ix == 0) {
							$(this).html(' <input type="text" name="Code" lay-verify="required" autocomplete="off" value="' + $(this).text().trim() + '" class="layui-input">');
						} else if(ix == 1) {
							$(this).html(' <input type="text" name="Name" lay-verify="required" autocomplete="off" value="' + $(this).text().trim() + '" class="layui-input">');
						} else if(ix == 2) {
							$(this).html(' <input type="text" name="Type" lay-verify="required" autocomplete="off" value="' + $(this).text().trim() + '" class="layui-input">');
						} else if(ix == 3) {
							$(this).html(' <input type="text" name="Length" lay-verify="required" autocomplete="off" value="' + $(this).text().trim() + '" class="layui-input">');
						} else if(ix == 4) {
							$(this).html(' <input type="text" name="Comment" lay-verify="required" autocomplete="off" value="' + $(this).text().trim() + '" class="layui-input">');
						}
					});
				}
			});
			$("#" + code + " .btn-delete").off("click").on("click", function(e) {
				var data = {};
				var _thisBtn = this;
				$(this).parent().parent().parent().find("td").each(function(ix) {
					if(ix == 0 || ix == 1 || ix == 2 || ix == 3 || ix == 4) {
						data[$(this).attr("name")] = $(this).text().trim();
					}
				});
				$.DBEditor.delField($("#dataModel").attr("data-file-id"), data, function(data) {
					// 移除行
					$(_thisBtn).parent().parent().parent().remove();
				});
			});
		},
		loadDB: function(id) {
			var _this = this;
			$.DBEditor.loadField(id, function(data) {
				$("#dataModel").attr("data-file-id", id);
				var fields = data;
				if(fields){
					for(var i = 0; i < fields.length; i++) {
						var editTr = '<tr id="' + fields[i].Code + '" >';
						editTr += '<td name="Code"> ' + fields[i].Code + '</td>';
						editTr += '<td name="Name"> ' + fields[i].Name + '</td>';
						editTr += '<td name="Type"> ' + fields[i].Type + '</td>';
						editTr += '<td name="Length"> ' + fields[i].Length + '</td>';
						editTr += '<td name="Comment"> ' + fields[i].Comment + '</td>';
						editTr += '<td >   <div class="layui-btn-group"> ';
						editTr += '<button class="layui-btn layui-btn-small btn-edit"><i class="layui-icon icon-bianji"></i></button>';
						editTr += '<button class="layui-btn layui-btn-small btn-delete"><i class="layui-icon icon-delete"></i></button>';
						editTr += '</div></td>';
						editTr += '</tr>';
						$("#dataModel tbody").append(editTr);
						_this.bindEvent(fields[i].Code);
					}
				}

			});
		}
	};

	var $ = layui.jquery;
	
	var id = $.Common.Utils.GetQueryString("id");
	$(".btn-add").click(function() {
		var fid = new Date().getTime();
		var editTr = '<tr id="'+fid+'" >';
		editTr += '<td name="Code"> <input type="text" name="Code" lay-verify="required" autocomplete="off" class="layui-input"></td>';
		editTr += '<td name="Name"> <input type="text" name="Name" lay-verify="required" autocomplete="off" class="layui-input"></td>';
		editTr += '<td name="Type"> <input type="text" name="Type" lay-verify="required" autocomplete="off" class="layui-input"></td>';
		editTr += '<td name="Length"> <input type="text" name="Length" lay-verify="required" autocomplete="off" class="layui-input"></td>';
		editTr += '<td name="Comment"> <input type="text" name="Comment" lay-verify="required" autocomplete="off" class="layui-input"></td>';
		editTr += '<td>   <div class="layui-btn-group"> ';
		editTr += '<button class="layui-btn layui-btn-small btn-new"><i class="layui-icon icon-duigou"  "></i></button>';
		editTr += '<button class="layui-btn layui-btn-small btn-delete"><i class="layui-icon icon-delete"></i></button>';
		editTr += '</div></td>';
		editTr += '</tr>';
		$("#dataModel tbody").append(editTr);
		controller.bindEvent(fid);
	});
	controller.loadDB(id);

});